﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class LapHoaDonBanSachBLL : BaseBLL
    {
        public class SachOnGui
        {
            int _IDSach,
                _SoLuong;
            float _DonGia;

            public int SoLuong
            {
                get { return _SoLuong; }
                set { _SoLuong = value; }
            }

            public float DonGia
            {
                get { return _DonGia; }
                set { _DonGia = value; }
            }

            public int IDSach
            {
                get { return _IDSach; }
                set { _IDSach = value; }
            }

            public SachOnGui()
            {
                _IDSach = -1;
                _SoLuong = 0;
                _DonGia = 0;
            }
        }

        DAL.DauSachDAO _DauSachDAO;
        BLL.QuiDinhBLL _QuiDinhBLL;
        DAL.KhachHangDAO _KhachHangDAO;
        DAL.HoaDonBanSachDAO _HoaDonBanSachDAO;
        DAL.ChiTietBanSachDAO _ChiTietBanSachDAO;

        public LapHoaDonBanSachBLL()
        {
            _DauSachDAO = new DAL.DauSachDAO();
            _QuiDinhBLL = new BLL.QuiDinhBLL();
            _KhachHangDAO = new DAL.KhachHangDAO();
            _HoaDonBanSachDAO = new DAL.HoaDonBanSachDAO();
            _ChiTietBanSachDAO = new DAL.ChiTietBanSachDAO();
        }

        private int CheckQD_2(
            System.Data.SqlClient.SqlConnection connection,
            System.Data.SqlClient.SqlTransaction transaction,
            ref DTO.DauSachDTO sachDto,
            int soluong)
        {
            if (_DauSachDAO.Select(connection, transaction, ref sachDto) < 0)
                return 1; // failed sach not exits on database
            else if (sachDto.LuongTon - soluong < _QuiDinhBLL.TonToiThieu)
                return 2; // failed ton toi thieuelse
            {
                sachDto.LuongTon -= soluong;
                return 0;
            }
        }

        private int CheckQD_2(
            System.Data.SqlClient.SqlConnection connection,
            System.Data.SqlClient.SqlTransaction transaction,
            ref DTO.KhachHangDTO khachDto,
            float phatsinh)
        {
            if (_KhachHangDAO.Select(connection, transaction, ref khachDto) < 0)
                return 3; // failed khach not exits on database
            else if (khachDto.DuNo+phatsinh > _QuiDinhBLL.NoToiDa)
                return 4; // failed no toi da
            else
            {
                khachDto.DuNo += phatsinh;
                return 0;
            }
        }

        public int LapPhieu(DateTime NgayLap, int IDKhachHang, List<SachOnGui> sachs,
            ref float duno)
        {
            int r = -1;
            try
            {
                System.Data.SqlClient.SqlConnection connection = OpenConnection();
                System.Data.SqlClient.SqlTransaction transaction = BeginTransaction();
                float phatsinh = 0;
                foreach (SachOnGui s in sachs)
                    phatsinh += (s.DonGia*s.SoLuong);

                DTO.KhachHangDTO khachDto = new DTO.KhachHangDTO();
                khachDto.ID = IDKhachHang;
                r = CheckQD_2(connection, transaction, ref khachDto, phatsinh);
                if (r != 0)
                {
                    Rollback();
                    CloseConnection();
                    return r;
                }
                // update du no khach hang
                r = _KhachHangDAO.Update(connection, transaction, khachDto);
                duno = khachDto.DuNo;

                // insert hoa don
                DTO.HoaDonBanSachDTO hoadonDto = new DTO.HoaDonBanSachDTO();
                hoadonDto.IDKhachHang = khachDto.ID;
                hoadonDto.NgayLap = NgayLap;
                r = _HoaDonBanSachDAO.Insert(connection, transaction, hoadonDto);
                hoadonDto.ID = r;
                DTO.ChiTietBanSachDTO chitietDto = new DTO.ChiTietBanSachDTO();
                chitietDto.IDHoaDon = hoadonDto.ID;
                foreach (SachOnGui s in sachs)
                {
                    DTO.DauSachDTO sachDto = new DTO.DauSachDTO();
                    sachDto.ID = s.IDSach;
                    r = CheckQD_2(connection, transaction, ref sachDto, s.SoLuong);
                    if (r != 0)
                    {
                        Rollback();
                        CloseConnection();
                        return r;
                    }
                    // update luong ton sach
                    r = _DauSachDAO.Update(connection, transaction, sachDto);
                    // insert chi tiet nhap sach
                    chitietDto.IDSach = sachDto.ID;
                    chitietDto.SoLuong = s.SoLuong;
                    chitietDto.DonGia = s.DonGia;
                    r = _ChiTietBanSachDAO.Insert(connection, transaction, chitietDto);
                }

                Commit();
                CloseConnection();
                return 0;
            }
            catch (System.Exception ex)
            {
                Rollback();
                CloseConnection();
                throw new System.Exception("LapPhieu failed " + ex.Message);
            }
            finally
            {
                CloseConnection();
            }
        }
    }
}
